package com.xiaolong.log.util;

import jakarta.servlet.http.HttpServletRequest;

public class HttpUtil {
    public static String method(HttpServletRequest request ){
        return request.getMethod();
    }
    public static String url(HttpServletRequest request ){
        return request.getRequestURL().toString();
    }
    public static String ip(HttpServletRequest request ){
        return getClientIp(request);
    }

    private static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
